package com.ruoyi.jetLinks.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResult2;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jetLinks.domain.NmEquipment;
import com.ruoyi.jetLinks.mapper.NmEquipmentMapper;
import com.ruoyi.jetLinks.service.INmEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息管理Controller
 * 
 * @author lxx
 * @date 2021-12-17
 */
@Api(tags = "设备信息管理")
@RestController
@RequestMapping("/jetLinks/nmEquipment")
public class NmEquipmentController extends BaseController {

    /**
     * 设置请求的统一前缀
     */
    @Value("${spring.profiles.active}")
    private String active;


    @Autowired
    private INmEquipmentService nmEquipmentService;

    @Autowired
    private NmEquipmentMapper nmEquipmentMapper;
    /**
     * 查询设备信息管理列表
     */
    @ApiOperation("查询设备信息管理列表")
    //@PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:list')")
    @GetMapping("/list")
    public TableDataInfo<NmEquipment> list(NmEquipment nmEquipment) {
        startPage();
        List<NmEquipment> list = nmEquipmentService.selectNmEquipmentList(nmEquipment);
        return getDataTable(list);
    }

    /**
     * 导出设备信息管理列表
     */
    @ApiOperation("导出设备信息管理列表")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:export')")
    @Log(title = "设备信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NmEquipment nmEquipment)
    {
        List<NmEquipment> list = nmEquipmentService.selectNmEquipmentList(nmEquipment);
        ExcelUtil<NmEquipment> util = new ExcelUtil<NmEquipment>(NmEquipment.class);
        util.exportExcel(response, list, "设备信息管理数据");
    }

    /**
     * 获取设备信息管理详细信息
     */
    @ApiOperation("获取设备信息管理详细信息")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult2<NmEquipment> getInfo(@PathVariable("id") Long id) {
        return AjaxResult2.success(nmEquipmentService.selectNmEquipmentById(id));
    }

    /**
     * 新增设备信息管理
     */
    @ApiOperation("新增设备信息管理")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:add')")
    @Log(title = "设备信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult2 add(@RequestBody NmEquipment nmEquipment) {
        return toAjax2(nmEquipmentService.insertNmEquipment(nmEquipment));
    }

    /**
     * 修改设备信息管理
     */
    @ApiOperation("修改设备信息管理")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:edit')")
    @Log(title = "设备信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult2 edit(@RequestBody NmEquipment nmEquipment) {
        return toAjax2(nmEquipmentService.updateNmEquipment(nmEquipment));
    }

    /**
     * 删除设备信息管理
     */
    @ApiOperation("删除设备信息管理")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:remove')")
    @Log(title = "设备信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult2 remove(@PathVariable Long[] ids) {
        return toAjax2(nmEquipmentService.deleteNmEquipmentByIds(ids));
    }

    /**
     * 更新二维码
     */
    @ApiOperation("更新二维码")
    @PreAuthorize("@ss.hasPermi('jetLinks:nmEquipment:qrcode')")
    @Log(title = "更新二维码", businessType = BusinessType.UPDATE)
    @GetMapping("/updateQrcode/{ids}")
    public AjaxResult updateQrcode(@PathVariable Long[] ids, HttpServletRequest request) {
        String ip = null;
        if(active.equals("hlw")){
            ip="101.133.173.241";
        }else{
            ip = IpUtils.getHostIp();
        }
        String url = "http://" + ip + ":" + request.getLocalPort();
        for(Long id : ids){
            NmEquipment nmEquipment = nmEquipmentMapper.selectById(id);
            if(StringUtils.isNotNull(nmEquipment)){
                nmEquipmentService.uptateNmEquipmentQrCode(nmEquipment,url);
            }
        }
        return AjaxResult.success("成功");
    }



}
